﻿<%@ Page Language="C#" MasterPageFile="~/Admin/admin.master" AutoEventWireup="true"
    CodeFile="GrnEdit.aspx.cs" Inherits="GrnEdit" Title="Grn Edit" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
    Grn - Add/Edit</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script src="myscript/GRNEditt.js" type="text/javascript"></script>

    <script language="javascript">
        function checkquantity(obj) {
            var quantity = parseFloat($(obj).val()); //Lay text TextBox
            var Poquantity = parseFloat($("[id$=HdPoQuantity]").val());
            var Errquantity = $("#Sperrquantity"); // Lay MarkupPercent
            if (quantity > Poquantity) {
                $(obj).val(Poquantity);
                //Errquantity.text('*');

            }
            else {
                //Errquantity.text('');
                CalculatePrice(obj);
            }
        }
        function CalculatePrice(obj) {
            var quantity = parseFloat($(obj).val()); //Lay text TextBox
            var costPrice = parseFloat($("[id$=CostPriceResult]").val());   // Lay Exchange Rate
            var total = parseFloat(quantity * costPrice);
            var textTotal = $("#sptotal"); // lay nguyen doi tuong
            textTotal.text(CommaFormatted(total, 0));
        }
        //xu ly thay  doi gia
        function CalculateChangePrice(obj) {

            var quantity = $("[id$=tbQuantity]"); // quantity
            CalculatePrice(quantity);
            var oldCost = parseFloat($("[id$=HFoldCost]").val());
            var newCost = parseFloat($(obj).val());
            if (oldCost == newCost) {
                $(obj).next().focus();
            }
            else {
                var sellPriceNew = $("[id$=SellPriceNew]"); // Lay SellPriceNew Rate
                var markupPercent = parseFloat($("#MarkupPercent").text()); // Lay MarkupPercent
                var costPrice = parseFloat($(obj).val());
                var sellPrice = parseFloat(costPrice * (markupPercent + 100) / 100);
                sellPriceNew.val(CommaFormatted(sellPrice));
                $(sellPriceNew).addClass("input-warning");
            }
        }

        //Ham Xu Ly Space Trong Sell Price
        function FillPrice(sender, e) {
            var key;
            if (window.event)
                key = window.event.keyCode;
            else if (e)
                key = e.which;
            else
                return true;
            if (key == 32) {
                var oldSellPrice = $("#SellPriceOld").text();
                $(sender).val(oldSellPrice);
                $(sender).removeClass("input-warning");
                return false;
            }
            return true;
        }
        function SetDefalutButton(e, buttonid) {
            var evt = e ? e : window.event;
            var quantity = parseFloat($("[id$=tbQuantity]").val()); //Lay text TextBox
            var Poquantity = parseFloat($("[id$=HdPoQuantity]").val());
            if (evt.keyCode == 13)/*Search if user hit enter key*/
            {
                if (quantity <= Poquantity) {
                    var bt = $('[id$=' + buttonid + ']');
                    //( bt[0].href);//if it is link button
                    bt.click(); //if it button
                    return false;
                    //alert(quantity);
                }
                // else
                //   alert('please check quantity');


            }
        }

        function checkValid() {
            var quantity = parseFloat($("[id$=tbQuantity]").val()); //Lay text TextBox
            var Poquantity = parseFloat($("[id$=HdPoQuantity]").val());
            var Errquantity = $("#Sperrquantity"); // Lay MarkupPercent
            if (quantity > Poquantity || quantity < 1) {
                ////                $(obj).val(Poquantity);
                Errquantity.text('*');
                return false;
            }
            else {
                Errquantity.text('');

                return true;
            }
        }
       
    </script>

    <data:MultiFormView ID="FormView1" DataKeyNames="GrnId" runat="server" DataSourceID="GrnDataSource"
        OnLoad="FormView1_Load" OnItemCommand="FormView1_ItemCommand" OnItemCreated="FormView1_ItemCreated">
        <EditItemTemplatePaths>
            <data:TemplatePath Path="~/Admin/UserControls/GrnFields.ascx" />
        </EditItemTemplatePaths>
        <InsertItemTemplatePaths>
            <data:TemplatePath Path="~/Admin/UserControls/GrnFields.ascx" />
        </InsertItemTemplatePaths>
        <EmptyDataTemplate>
            <b>Grn not found!</b>
        </EmptyDataTemplate>
        <FooterTemplate>
            <asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                CssClass="button" Text="Insert" ValidationGroup="Main" />
            <asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                CssClass="button" Text="Update" ValidationGroup="Main" />
            <asp:Button ID="PreviewButton" runat="server" CausesValidation="True" Visible="False"
                ToolTip="Print Preview" CssClass="button" Text="Preview" CommandName="Preview"
                ValidationGroup="Main" />
            <asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                ToolTip="Return To Manage" CssClass="button" Text="Finish" ValidationGroup="Main" />
            <asp:Button ID="CloseGrn" runat="server" CausesValidation="False" Visible="false"
                CommandName="CloseGrn" ToolTip="Import GRN To Stock" CssClass="button" Text="Import To Stock"
                OnClientClick="return confirm('After import cannot change GRN info!');" ValidationGroup="Main" />
        </FooterTemplate>
    </data:MultiFormView>
    <data:GrnDataSource ID="GrnDataSource" runat="server" SelectMethod="GetByGrnId">
        <Parameters>
            <asp:QueryStringParameter Name="GrnId" QueryStringField="GrnId" Type="String" />
        </Parameters>
    </data:GrnDataSource>
    <br />
    <br />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <asp:Panel runat="server" ID="pn_GrnDetailsAdd" Width="100%" Visible="false" ValidationGroup="add"
                DefaultButton="btnSave" DefaultFocus="dataDrugId">
                <table>
                    <tr>
                        <td colspan="9">
                            <h3>
                                Add Details
                            </h3>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Drug Id:<span id="UnitResult" style="color: #CC6600; font-weight: bold; min-width: 40px;"
                                title="Unit"> </span><span id="MarkupPercent" style="color: #CC6600; font-weight: bold;
                                    min-width: 40px" title="MarkupPercent"></span>
                        </td>
                        <td>
                            Cost Price:
                        </td>
                        <td>
                            SellPrice Old:
                        </td>
                        <td>
                            SellPrice New:
                        </td>
                        <td>
                            BatchLot:
                        </td>
                        <td>
                            ExpDate:
                        </td>
                        <td>
                            Quantity:
                        </td>
                        <td style="width: 5px">
                        </td>
                        <td>
                            Remark:
                        </td>
                        <td>
                            Total:
                        </td>
                        <td>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
                                <data:EntityDropDownList runat="server" ID="dataDrugId" DataTextField="DrugID" DataValueField="DrugId"
                                    AppendNullItem="true" Required="true" TabIndex="10" NullItemText="< Please Choose ...>"
                                    ErrorText="Required" onchange="GetDrug();" ValidationGroup="add" />
                                <asp:Button ID="Button1" runat="server" Text="Button" Visible="false" />
                            </asp:Panel>
                        </td>
                        <td>
                            <input type="text" runat="server" id="CostPriceResult" class="text-input" tabindex="11"
                                validationgroup="add" onblur="javascript:CalculateChangePrice(this);" value="0"
                                style="width: 80px" />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="CostPriceResult"
                                runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                            <asp:HiddenField runat="server" ID="HFoldCost" Value='' />
                        </td>
                        <td>
                            <span id="SellPriceOld" style="color: #CC6600; font-weight: bold; min-width: 40px">
                            </span>
                        </td>
                        <td>
                            <input type="text" runat="server" id="SellPriceNew" class="text-input" tabindex="11"
                                validationgroup="add" value="" style="width: 80px" onkeypress="javascript:return FillPrice(this,event);" />
                            <asp:RequiredFieldValidator ID="RequiredFieldsellpricenew" ControlToValidate="SellPriceNew"
                                runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
                        </td>
                        <td align="left">
                            <input type="text" runat="server" id="TxtBatchLot" class="text-input" tabindex="11"
                                value="" style="width: 70px" />
                        </td>
                        <td align="left">
                            <input type="text" runat="server" id="txtExpDate" class="text-input datepicker" tabindex="11"
                                value="" style="width: 70px" />
                        </td>
                        <td>
                            <input type="text" runat="server" id="tbQuantity" class="text-input" tabindex="11"
                                validationgroup="add" value="1" style="width: 30px" />
                            <asp:RequiredFieldValidator ID="ReqVal_dataQuantity" runat="server" Display="Dynamic"
                                ControlToValidate="tbQuantity" ErrorMessage="Required" ValidationGroup="add"></asp:RequiredFieldValidator>
                            <asp:HiddenField ID="HdPoQuantity" runat="server" Value="" />
                        </td>
                        <td style="width: 5px">
                            <span id="Sperrquantity"></span>
                        </td>
                        <td>
                            <input type="text" runat="server" id="tbRemark" class="large-input text-input" tabindex="12" />
                        </td>
                        <td>
                            <span id="sptotal" style="color: #CC6600; font-weight: bold; min-width: 40px"></span>
                        </td>
                        <td>
                            <asp:Button ID="btnSave" Text="Add" class="button" TabIndex="13" runat="server" OnClick="btnSave_Click"
                                ValidationGroup="add" OnClientClick="return checkValid();" />
                            <input type="button" id="btnCancel" value="Cancel" class="button" style="display: none"
                                validationgroup="add" tabindex="14" />
                        </td>
                    </tr>
                </table>
            </asp:Panel>
            <%--<a href="#<%#Eval("GrnDetailId") %>" onclick="DeleteRow(this,<%#Eval("GrnDetailId") %>);"
                        style="display: <%#(Boolean)Eval("GrnIdSource.IsComplete")?"none":"inherit" %>">
                        Delete</a>--%>
            <data:EntityGridView ID="GridViewGrnDetails1" runat="server" AutoGenerateColumns="False"
                OnSelectedIndexChanged="GridViewGrnDetails1_SelectedIndexChanged" DataSourceID="GrnDetailsDataSource1"
                DataKeyNames="GrnDetailId" AllowMultiColumnSorting="false" DefaultSortColumnName=""
                DefaultSortDirection="Ascending" ExcelExportFileName="Export_GrnDetails.xls"
                Visible='<%# FormView1.DefaultMode != FormViewMode.Insert %>'>
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton runat="server" ID="btn_Delete" OnClick="btn_Delete_Click" Visible='<%#!(Boolean)Eval("GrnIdSource.IsComplete")%>'
                                ValidationGroup="delete">Delete</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="DrugId" HeaderText="DrugId" SortExpression="[DrugId]" />
                    <data:HyperLinkField HeaderText="Drug Id" DataNavigateUrlFormatString="DrugDispoEdit.aspx?DrugId={0}"
                        DataNavigateUrlFields="DrugId" DataContainer="DrugIdSource" DataTextField="DrugName" />
                    <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="[Quantity]"
                        DataFormatString="{0:#,##0}">
                        <ItemStyle HorizontalAlign="Right" />
                    </asp:BoundField>
                    <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" SortExpression="[UnitPrice]"
                        DataFormatString="{0:#,##0.00}">
                        <ItemStyle HorizontalAlign="Right" />
                    </asp:BoundField>
                    <asp:BoundField DataField="ExpDate" HeaderText="Exp Date" SortExpression="[ExpDate]"
                        DataFormatString="{0:MMM-yyyy}" />
                    <asp:BoundField DataField="BatchLot" HeaderText="Batch Lot" SortExpression="[BatchLot]" />
                    <asp:BoundField DataField="Remark" HeaderText="Remark" SortExpression="[Remark]" />
                </Columns>
                <EmptyDataTemplate>
                    <b>No Grn Details Found! </b>
                </EmptyDataTemplate>
            </data:EntityGridView>
            <data:GrnDetailsDataSource ID="GrnDetailsDataSource1" runat="server" SelectMethod="GetPaged"
                EnableSorting="True" EnableDeepLoad="True">
                <DeepLoadProperties Method="IncludeChildren" Recursive="False">
                    <Types>
                        <data:GrnDetailsProperty Name="DrugDispo" />
                        <data:GrnDetailsProperty Name="Grn" />
                    </Types>
                </DeepLoadProperties>
                <Parameters>
                    <data:SqlParameter Name="WhereClause" UseParameterizedFilters="false">
                        <Filters>
                            <data:GrnDetailsFilter Column="GrnId" QueryStringField="GrnId" ComparisionType="Equals" />
                        </Filters>
                    </data:SqlParameter>
                </Parameters>
            </data:GrnDetailsDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
    <br />
</asp:Content>
